package com.suanfa.collect;

import com.suanfa.util.ArrayUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author jukg
 * @date 2025/01/15 11:05
 * @description  根据小明身高差排序，小的在前 大的在后， 相同则值小的在前
 */
public class Abs {

    public static void main(String[] args) {
        int[] ss = {95,96,97,98,101,102,103,104,195};
        int h = 100;
        List<Integer>  r = sort(h, ss);
        for(Integer i : r){
            System.out.print(i+",");
        }
    }



    public static List<Integer> sort(int h, int[] mates){

        List<Integer> list = new ArrayList<>();
        for(int m : mates){
            list.add(m);
        }

        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {

                int diffa = Math.abs(h- o1);
                int diffb = Math.abs(h- o2);
                if(diffa == diffb){
                    return  o1 -o2;
                }
                return diffa -diffb;
            }
        });

        return list;
    }


}
